import { isStringArray } from "@/utils";

export const setTextStatus = (textProps, text) => {
  textProps.status = text;
}

export const removeOption = (optionProps, index) => {
  if (optionProps.status.length === 2) {
    return false;
  }
  optionProps.status.splice(index, 1);
  return true;
}

export const addOption = (optionProps) => {
  if (isStringArray(optionProps.status)) {
    const statusArray = optionProps.status;
    const lastItem = statusArray[statusArray.length - 1]; // 获取最后一个字符串
    const lastChar = lastItem.slice(-1); // 获取最后一位字符
    // 检查是否是数字
    if (!isNaN(lastChar) && lastChar.trim() !== '') {
      optionProps.status.push('新增选项' + (parseInt(lastChar) + 1));
    } else {
      optionProps.status.push('新增选项');
    }
  }
}

export const setPosition = (optionProps, payload) => {
  optionProps.currentStatus = payload;
}


export const setFontSize = (optionProps, payload) => {
  optionProps.currentStatus = payload;
}

export const setFontWeight = (optionProps, payload) => {
  optionProps.currentStatus = payload;
}

export const setFontItalic = (optionProps, payload) => {
  optionProps.currentStatus = payload;
}

export const setFontColor = (optionProps, payload) => {
  optionProps.status = payload;
}

export const setOptionLink = (optionProps, payload) => {
  optionProps.status[payload.index].value = payload.link
}

export const setDateTime = (optionProps, payload) => {
  optionProps.currentStatus = payload;

}

export const addPicOption = (optionProps) => {
  const statusArray = optionProps.status;

  if (!statusArray || statusArray.length === 0) {
    optionProps.status.push({
      picTitle: '图片标题1',
      picDesc: '图片描述1',
      value: '',
    });
    return;
  }

  const lastItem = statusArray[statusArray.length - 1];
  const lastTitleItem = lastItem.picTitle;
  const lastDescItem = lastItem.picDesc;

  // 尝试从标题或描述中提取数字
  const extractNumber = (str) => {
    const match = str.match(/(\d+)$/);
    return match ? parseInt(match[0]) : null;
  };

  const titleNum = extractNumber(lastTitleItem);
  const descNum = extractNumber(lastDescItem);

  // 使用较大的数字+1确保连续性
  const nextNum = Math.max(titleNum || 0, descNum || 0) + 1;

  optionProps.status.push({
    picTitle: titleNum ? `图片标题${nextNum}` : '新增图片标题',
    picDesc: descNum ? `图片描述${nextNum}` : '新增图片描述',
    value: '',
  });
}

export function setTextType(optionProps, payload) {
  optionProps.currentStatus = payload;
}

export function setOptionIsUse(optionProps, payload) {
  optionProps.isUse = payload;
}

